﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.Base.FlexField.ValueSet;
using UFIDA.U9.MRP.Enums;
using UFIDA.U9.SM.Ship;
using UFIDA.U9C.Cust.ZL.PlugBE.Tool;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;

namespace UFIDA.U9C.Cust.ZL.PlugBE
{
    /// <summary>
    /// 审核后的采购订单推送到SRM
    /// </summary>
    class POCheckedZLExtend : IEventSubscriber
    {
        public void Notify(params object[] args)
        {
            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent))
                return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;
            if (key == null)
                return;
            UFIDA.U9.PM.PO.PurchaseOrder entity = key.GetEntity() as UFIDA.U9.PM.PO.PurchaseOrder;
            if (entity == null)
                return;
            try
            {
                if (PubWebClass.IsOpenPlug())
                {
                    if (entity.OriginalData.Status == U9.PM.PO.PODOCStatusEnum.Approving && entity.Status == U9.PM.PO.PODOCStatusEnum.Approved)
                    {
                        //采购增加了一个单据类型，特殊物料采购，U9同步SRM的接口要把“特殊物料采购”和“零星物料采购”这两个单据类型去掉，不同步SRM；SRM同步U9的单据类型只对应“零星物料采购”
                        if (!entity.DocumentType.Name.Contains("特殊物料采购") && !entity.DocumentType.Name.Contains("零星物料采购") && !entity.DocumentType.Name.Contains("直运采购订单"))
                        {
                            UFIDA.U9C.Cust.ZL.API.BaseData.Proxy.DataOperateProxy proxy = new API.BaseData.Proxy.DataOperateProxy();
                            proxy.IDs = new List<long> { entity.ID };
                            proxy.Operate = 1;
                            proxy.Type = "11";
                            API.BaseData.ReturnDTOData res = proxy.Do();
                            if (res.Code == 2)
                            {
                                throw new Exception(res.Message + "" + res.Data);
                            }
                            else
                            {
                                PubWebClass.Log("同步采购订单-》SRM成功【" + entity.DocNo + "】；");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {

                PubWebClass.Log("同步采购订单->SRM异常【" + entity.DocNo + "】：" + ex.Message);
                throw new NotImplementedException("采购订单->SRM失败：" + ex.Message);
            }

        }


    }
}
